package simplyrugby.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import simplyrugby.entity.Match;
import simplyrugby.mapper.MatchMapper;
import simplyrugby.mapper.TeamMapper;
import simplyrugby.service.MatchService;

import java.util.List;

/**
* @author admin
* @description 针对表【match(比赛)】的数据库操作Service实现
* @createDate 2025-03-29 18:37:29
*/
@Service
@RequiredArgsConstructor
public class MatchServiceImpl extends ServiceImpl<MatchMapper, Match>
    implements MatchService{

    private final TeamMapper teamMapper;
    @Override
    public Page<Match> list(Integer pageNum, Integer pageSize, Match match) {
        LambdaQueryWrapper<Match> wrapper = Wrappers.lambdaQuery(Match.class)
                .eq(!ObjectUtils.isEmpty(match.getId()), Match::getId, match.getId())
                .and(!ObjectUtils.isEmpty(match.getFirstTeamId()) || !ObjectUtils.isEmpty(match.getSecondTeamId()),
                        q -> q.eq(!ObjectUtils.isEmpty(match.getFirstTeamId()), Match::getFirstTeamId, match.getFirstTeamId())
                                .or()
                                .eq(!ObjectUtils.isEmpty(match.getSecondTeamId()), Match::getSecondTeamId, match.getSecondTeamId())
                )
                .like(!ObjectUtils.isEmpty(match.getMatchName()), Match::getMatchName, match.getMatchName());


        Page<Match> page = new Page<>(pageNum, pageSize);
        this.page(page, wrapper);
        List<Match> list = page.getRecords().stream().map(u -> this.details(u.getId()) ).toList();
        page.setRecords(list);
        return page;

    }

    @Override
    public Match details(String id) {
        Match match = baseMapper.selectById(id);
        match.setFirstTeam(teamMapper.selectById(match.getFirstTeamId()));
        match.setSecondTeam(teamMapper.selectById(match.getSecondTeamId()));
        return match;
    }

}




